package com.thermal.thermal_inspection.controller;

import com.thermal.thermal_inspection.entity.Case;
import com.thermal.thermal_inspection.entity.CaseInfo;
import com.thermal.thermal_inspection.service.CaseInfoService;
import com.thermal.thermal_inspection.service.CaseService;
import com.thermal.thermal_inspection.vo.CaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cases")
public class CaseController {

    @Autowired
    private CaseService caseService;
    @Autowired
    private CaseInfoService caseInfoService;

    @GetMapping("/{id}")
    public ResponseEntity<CaseVo> getCaseWithInfo(@PathVariable int id) {
        Case caseData = caseService.getCaseById(id);
        if (caseData != null) {
            List<CaseInfo> caseInfos = caseInfoService.getByCaseId(id); // 查询 caseInfo 列表
            CaseVo caseVo = new CaseVo();
            caseVo.setCaseId(caseData.getCaseId());
            caseVo.setCaseTime(caseData.getCaseTime());
            caseVo.setCaseName(caseData.getCaseName());
            caseVo.setPatientId(caseData.getPatientId());
            caseVo.setDoctorId(caseData.getDoctorId());
            caseVo.setCaseInfos(caseInfos);
            return ResponseEntity.ok(caseVo);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping
    public List<Case> getAllCases() {
        return caseService.getAllCases();
    }

    @PostMapping
    public ResponseEntity<Object> createCase(@RequestBody Case caseData) {
        caseData.setCaseTime(new Timestamp(System.currentTimeMillis()));
        int result = caseService.addCase(caseData);
        if (result > 0) {
            return ResponseEntity.ok().body(Map.of(
                    "message", "新增成功",
                    "caseId", caseData.getCaseId()
            ));
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("message", "新增失败"));
        }
    }


    @PutMapping
    public ResponseEntity<String> updateCase(@RequestBody Case caseData) {
        if (caseService.updateCase(caseData)) {
            return ResponseEntity.ok("更新成功");
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("更新失败");
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteCase(@PathVariable int id) {
        if (caseService.deleteCase(id)) {
            return ResponseEntity.ok("删除成功");
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("删除失败");
    }
    // 模糊查询接口：根据 patientId 和 doctorId 查询
    @GetMapping("/search")
    public ResponseEntity<List<Case>> searchCases(@RequestParam(required = false) Integer patientId,
                                  @RequestParam(required = false) Integer doctorId) {
        return ResponseEntity.ok(caseService.searchCases(patientId, doctorId));
    }
}

